﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.UserControls;

    public class AuditList : BasePage
    {
        protected Button btnExport;
        protected Button btnGoBack;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F1222;
        protected GridView gvAuditData;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblWh;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2AuditDataList(AuditDataQueryEntity auditDataQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(auditDataQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(auditDataQuery, "AUDIT_DATA");
            base.BindData2GridView(this.gvAuditData, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            AuditDataQueryEntity auditQuery = this.GetAuditDefinitionQueryEntity();
            auditQuery.IsGetAll = true;
            string currentPageIndex = auditQuery.CurrentPage;
            auditQuery.CurrentPage = "0";
            this.BindData2AuditDataList(auditQuery);
            this.gvAuditData.ShowFooter = false;
            auditQuery.IsGetAll = false;
            auditQuery.CurrentPage = currentPageIndex;
            this.SetAuditDataQueryEntity(auditQuery);
            base.Export2Excel("AuditData.xls", this.gvAuditData);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            AuditDataQueryEntity auditQuery = this.GetAuditDefinitionQueryEntity();
            auditQuery.CurrentPage = "0";
            this.BindData2AuditDataList(auditQuery);
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("AuditQuery.aspx", false);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            AuditDataQueryEntity auditQuery = this.GetAuditDefinitionQueryEntity();
            auditQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2AuditDataList(auditQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            AuditDataQueryEntity auditQuery = this.GetAuditDefinitionQueryEntity();
            auditQuery.TableName = "";
            this.BindData2AuditDataList(auditQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            AuditDataQueryEntity auditQuery = this.GetAuditDefinitionQueryEntity();
            if (int.Parse(auditQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                auditQuery.CurrentPage = (int.Parse(auditQuery.CurrentPage) + 1).ToString();
                this.BindData2AuditDataList(auditQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            AuditDataQueryEntity auditQuery = this.GetAuditDefinitionQueryEntity();
            if (int.Parse(auditQuery.CurrentPage) > 0)
            {
                auditQuery.CurrentPage = (int.Parse(auditQuery.CurrentPage) - 1).ToString();
                this.BindData2AuditDataList(auditQuery);
            }
        }

        private AuditDataQueryEntity GetAuditDefinitionQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as AuditDataQueryEntity);
            }
            return (base.PageQueryEntitySession as AuditDataQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                AuditDataQueryEntity auditQuery = this.GetAuditDefinitionQueryEntity();
                auditQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2AuditDataList(auditQuery);
            }
        }

        protected void gvAuditData_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F1222", this.gvAuditData);
        }

        protected void gvAuditData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvAuditData_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                AuditDataQueryEntity auditQuery = this.GetAuditDefinitionQueryEntity();
                auditQuery.SortField = e.SortExpression;
                if (auditQuery.SortDirection == "ASC")
                {
                    auditQuery.SortDirection = "DESC";
                }
                else if (auditQuery.SortDirection == "DESC")
                {
                    auditQuery.SortDirection = "ASC";
                }
                this.BindData2AuditDataList(auditQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            AuditDataQueryEntity auditDataQuery = new AuditDataQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    auditDataQuery.CreatedBy = this.PreviousPage.CreateBy;
                    auditDataQuery.TableName = this.PreviousPage.TableName;
                    auditDataQuery.ColName = this.PreviousPage.ColName;
                    auditDataQuery.StartTime = this.PreviousPage.StartTime;
                    auditDataQuery.EndTime = this.PreviousPage.EndTime;
                    auditDataQuery.PrimaryKey = this.PreviousPage.PrimaryKey;
                    auditDataQuery.ForInsert = this.PreviousPage.Insert;
                    auditDataQuery.ForUpdate = this.PreviousPage.Update;
                    auditDataQuery.ForDelete = this.PreviousPage.Delete;
                }
                else
                {
                    auditDataQuery.CreatedBy = "";
                    auditDataQuery.TableName = "";
                    auditDataQuery.ColName = "";
                    auditDataQuery.StartTime = "";
                    auditDataQuery.EndTime = "";
                    auditDataQuery.PrimaryKey = "";
                    auditDataQuery.ForInsert = "";
                    auditDataQuery.ForUpdate = "";
                    auditDataQuery.ForDelete = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            auditDataQuery.IsGetAll = false;
            auditDataQuery.CurrentPage = "0";
            auditDataQuery.PageSize = this.gvAuditData.PageSize.ToString();
            auditDataQuery.SortField = "ADA.TABLE_NAME";
            auditDataQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvAuditData.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetAuditDataQueryEntity(auditDataQuery);
            this.BindData2AuditDataList(auditDataQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetAuditDataQueryEntity(AuditDataQueryEntity auditDataQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = auditDataQuery;
            }
            else
            {
                base.PageQueryEntitySession = auditDataQuery;
            }
        }

        public AuditQuery PreviousPage
        {
            get
            {
                return (AuditQuery) base.PreviousPage;
            }
        }
    }
}

